Ext.define('PJ.view.registrationmanagement.APreChooseUserList', {
    extend: 'AM.default.DefaultList',
    
    alias: ['widget.signUpPreChooseUserList'],
    
    title: '待选人员列表',
    
    queryUnitId: null,
//    height: 269,
    
    columns: [{
        header: '姓名',
        dataIndex: 't.user_name',
        width: 110
    }/*, {
        header: '性别',
        dataIndex: 'bb.data_name',
        width: 60
    },{
        header: '年龄',
        dataIndex: 't.birthday',
        width: 100
    }*/,{
        header: '所在单位',
        dataIndex: 'ud.depname',
        flex: 1,
        minWidth: 100,
        renderer: function(value, metaData, record){
            metaData.tdAttr = 'data-qtip="' + value + '"';
            return value;
        }
    }/*, {
        header: '单位及职务',
        dataIndex: 't.duty_remark',
        flex: 1,
        minWidth: 100,
        renderer: function(value, metaData, record){
            metaData.tdAttr = 'data-qtip="' + value + '"';
            return value;
        }
    },{
        header: '职级',
        dataIndex: 'ub.data_name',
        flex: 1,
        minWidth: 100,
        renderer: function(value, metaData, record){
            metaData.tdAttr = 'data-qtip="' + value + '"';
            return value;
        }
    }*/, {
        header: '学员帐号',
        dataIndex: 't.login_id',
        width: 90,
        renderer: function(value, metaData, record){
//            metaData.tdAttr = 'data-qtip="' + value + '"';
            return value;
        }
    }
    /*
    ,{
        header: '手机号',
        dataIndex: 't.phoneNumber',
        width: 100
    }*/
    ],
    
    initComponent: function(){
        this.store = Ext.create('PJ.' + prototype + '.registrationmanagement.APreChooseUserList');
        
        this.callParent(arguments);
    },
    
    onAddClick: function(button){
        var me = this;
        var records = this.getSelectionModel().getSelection();
        
        var userIds = new Array();
        Ext.Array.each(records, function(name, index){
        	userIds.push(name.get("userId"));
        });
        me.loadMask.show();
        if(gj_onlineLearning_constant.loadType == "jsonp"){
        	var storeToken = Ext.create('AM.default.jsonp.DefaultAjaxStore',{
    			loadType: gj_onlineLearning_constant.loadType,
                url: gj_onlineLearning_constant.tokenUrl
    		});
        	storeToken.load(function(rec, operation, success){
    			var token = operation.response;
                var classIds = new Array();
                Ext.Array.each(records, function(name, index){
                	classIds.push(name.get("classId"));
                });
                var store = Ext.create('AM.default.jsonp.DefaultAjaxStore',{
					loadType: gj_onlineLearning_constant.loadType,
					url: me.store.addUserUrl,
					extraParams: {
						searchUserIds: userIds,
                        //queryUnitId: me.queryUnitId,
                        searchClassID: me.up("window").trainingGradeId,
                        searchApplyOrganCode: me.up("window").searchApplyOrganCode,
                        "springmvc.TOKEN": token
					}
				});
				store.load(function(rec, operation, success){
					me.loadMask.hide();
					if(success){
						Ext.Msg.alert('消息', '添加成功！', function(){
                            var store = me.getStore();
                            store.load();
                            
                            // 刷新已选面板
                            me.up("window").down("signUpChoosedUserList").getStore().load();
                            me.up("window").grid.getStore().load();
                        });
					}else{
						Orchid.alert('消息', operation.error);
					}
				});
        	});
        }else if(gj_onlineLearning_constant.loadType == "ajax"){
        	Ext.Ajax.request({
                url: tokenUrl,
                success: function(response){
                    var data = response.responseText;
                    var token = Ext.decode(data, true);
                    Ext.Ajax.request({
                        url: me.store.addUserUrl,
                        params: {
                        	searchUserIds: userIds,
                            //queryUnitId: me.queryUnitId,
                            searchClassID: me.up("window").trainingGradeId,
                            searchApplyOrganCode: me.up("window").searchApplyOrganCode,
                            "springmvc.TOKEN": token
                        },
                        success: function(response){
                            me.loadMask.hide();
                            var data = response.responseText;
                            var json = Ext.decode(data, true);
                            if (json.success) {
//                                Ext.Msg.alert('消息', json.msg, function(){
                                Ext.Msg.alert('消息', '添加成功！', function(){
                                    var store = me.getStore();
                                    store.load();
                                    
                                    // 刷新已选面板
                                    me.up("window").down("signUpChoosedUserList").getStore().load();
                                    me.up("window").grid.getStore().load();
                                });
                            }
                            else {
                                Ext.Msg.alert('消息', json.msg, function(){
                                    var store = me.getStore();
                                    store.load();
                                    me.up("window").grid.getStore().load();
                                });
                            }
                        }
                    });
                }
            });
        }
    },
    onAddClick: function(button){
        var me = this;
        var records = this.getSelectionModel().getSelection();
        
        var userIds = new Array();
        Ext.Array.each(records, function(name, index){
        	userIds.push(name.get("userId"));
        });
        me.loadMask.show();
        if(gj_onlineLearning_constant.loadType == "jsonp"){
        	var storeToken = Ext.create('AM.default.jsonp.DefaultAjaxStore',{
    			loadType: gj_onlineLearning_constant.loadType,
                url: gj_onlineLearning_constant.tokenUrl
    		});
        	storeToken.load(function(rec, operation, success){
    			var token = operation.response;
                var classIds = new Array();
                Ext.Array.each(records, function(name, index){
                	classIds.push(name.get("classId"));
                });
                var store = Ext.create('AM.default.jsonp.DefaultAjaxStore',{
					loadType: gj_onlineLearning_constant.loadType,
					url: me.store.addUserUrl,
					extraParams: {
						searchUserIds: userIds,
                        //queryUnitId: me.queryUnitId,
                        searchClassID: me.up("window").trainingGradeId,
                        searchApplyOrganCode: me.up("window").searchApplyOrganCode,
                        "springmvc.TOKEN": token
					}
				});
				store.load(function(rec, operation, success){
					me.loadMask.hide();
					if(success){
						Ext.Msg.alert('消息', '添加成功！', function(){
                            var store = me.getStore();
                            store.load();
                            
                            // 刷新已选面板
                            me.up("window").down("signUpChoosedUserList").getStore().load();
                            me.up("window").grid.getStore().load();
                        });
					}else{
						Orchid.alert('消息', operation.error);
					}
				});
        	});
        }else if(gj_onlineLearning_constant.loadType == "ajax"){
        	Ext.Ajax.request({
                url: tokenUrl,
                success: function(response){
                    var data = response.responseText;
                    var token = Ext.decode(data, true);
                    Ext.Ajax.request({
                        url: me.store.addUserUrl,
                        params: {
                        	searchUserIds: userIds,
                            //queryUnitId: me.queryUnitId,
                            searchClassID: me.up("window").trainingGradeId,
                            searchApplyOrganCode: me.up("window").searchApplyOrganCode,
                            "springmvc.TOKEN": token
                        },
                        success: function(response){
                            me.loadMask.hide();
                            var data = response.responseText;
                            var json = Ext.decode(data, true);
                            if (json.success) {
//                                Ext.Msg.alert('消息', json.msg, function(){
                                Ext.Msg.alert('消息', '添加成功！', function(){
                                    var store = me.getStore();
                                    store.load();
                                    
                                    // 刷新已选面板
                                    me.up("window").down("signUpChoosedUserList").getStore().load();
                                    me.up("window").grid.getStore().load();
                                });
                            }
                            else {
                                Ext.Msg.alert('消息', json.msg, function(){
                                    var store = me.getStore();
                                    store.load();
                                    me.up("window").grid.getStore().load();
                                });
                            }
                        }
                    });
                }
            });
        }
    },
    listeners: {
        selectionchange: function(selModel, selected){
            this.down('#' + this.oldId + "Add").setDisabled(selected.length === 0);
			this.down('#' + this.oldId + "ReAdd").setDisabled(selected.length === 0);
        },
        viewready: function(){
			var me = this;
			var flag = me.up("window").flag;
    		this.addDocked([{
    			dock: 'top',
    			xtype: "searchform",
    			columnCount: 2,
    			collapsedFieldSet: true,
    			fieldDefaults: {
    				labelAlign: 'right',
    				labelWidth: 100
    			},
    			items: [{
    				xtype: "textfield",
    				fieldLabel: '姓名',
    				name: "searchUserName",
    				maxLength: "10",
    				emptyText: "请输入姓名"
    			},{
    				xtype: "textfield",
    				fieldLabel: '学员帐号',
    				name: "searchLoginID",
    				maxLength: "50",
					//regex: /^[0-9]\d{0,}$/,
                    //regexText: '请输入整数！',
    				emptyText: "请输入学员帐号"
    			},{
    				xtype: "textfield",
    				fieldLabel: '所在单位',
    				name: "searchDepName",
    				maxLength: "50",
    				emptyText: "请输入所在单位"
    			}],
    			onResetClick: function(button){
                    var form = button.up('form');
                    var gridPanel = form.grid;
                    var newParams = {
                    		searchDepId: gridPanel.queryUnitId,
                    		searchClassID: gridPanel.up("window").trainingGradeId
                    };
                    gridPanel.getStore().proxy.extraParams = {};
                    
                    // 重置查询
                    // gridPanel.down("activeCombo").reset();
                    // 4.0强制更改store参数写法
                    var params = gridPanel.getStore().proxy.extraParams;
                    Ext.apply(params, newParams);
                    
                    form.getForm().reset();
                },
    			grid: this
    		},{
    			xtype: 'toolbar',
    			dock: 'top',
    			displayInfo: true,
    			items: [
    	        {
    	        	text: '报名',
    	            iconCls: 'planPersonIcon',
    	            itemId: this.oldId + "Add",
    	            scope: this,
					hidden:flag,
    	            disabled: true,
    	            handler: function(button){
    	                button.up("grid").onAddClick(button);
    	            }
    	        }, "->", "->",this.countSelectPanel]
    		}]);
    		// 触发权限过滤事件
    		this.fireEvent("permission", this);
    	},
    	afterrender: function(){
            this.loadMask = new Ext.LoadMask(this, {
                msg: "正在加载中,请稍后...."
            });
        }
    }
});
